package cn.mdx.common.filters;

import cn.mdx.common.beans.ResultBean;
import cn.mdx.common.beans.User;
import cn.mdx.common.exceptions.NoPermissionException;
import cn.mdx.common.exceptions.UnloginException;
import cn.mdx.common.utils.FilterUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = "/need/*")
public class NeedFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpSession session = ((HttpServletRequest) request).getSession();
        User user = (User) session.getAttribute("user");
        if( user==null ) {
            FilterUtil.json(request,response,ResultBean.NO_LOGIN,"未登录");
        } else if( user.getPower()!=4 ) {
            FilterUtil.json(request,response,ResultBean.NO_PERMISSION,"没有权限");
        } else {
            chain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}
